package com.spring.cloud.module.common.core;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.logging.log4j.LogManager;

public class BaseJsonObject extends BaseObject {

    private static final long serialVersionUID = 5138769665130336120L;

    /**
     * 将对象打印成json格式的字符串输出
     * @return
     */
    private String toJsonString() {
        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,false);

        String jsonString = "";
        try {
            jsonString = mapper.writeValueAsString(this);
        } catch (JsonProcessingException e) {
            LogManager.getLogger(this).error(e.getMessage(), e);
        }
        return jsonString;
    }

    @Override
    public String toString() {
        return  toJsonString();
    }
}
